package cn.com.thingo.shyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.shyintelligent.base.RxPresenter;
import cn.com.thingo.shyintelligent.base.contract.MessageContract;
import cn.com.thingo.shyintelligent.model.DataManager;
import cn.com.thingo.shyintelligent.model.bean.CommonBean;
import cn.com.thingo.shyintelligent.model.bean.MessageListBean;
import cn.com.thingo.shyintelligent.util.LogUtil;
import cn.com.thingo.shyintelligent.util.RxUtil;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;

public class MessagePresenter extends RxPresenter<MessageContract.View> implements MessageContract.Presenter {
    private DataManager dataManager;

    @Inject
    public MessagePresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }

    @Override
    public void messageList(String token) {
        addSubscribe(dataManager.messageList(token)
                .compose(RxUtil.<MessageListBean>rxSchedulerHelper())
                .subscribe(new Consumer<MessageListBean>() {
                    @Override
                    public void accept(MessageListBean messageListBean) throws Exception {
                        if (messageListBean.getCode() == 0) {
                            mView.viewMessageList(messageListBean);
                        } else {
                            ToastUtil.shortShow(messageListBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));

    }

    @Override
    public void readMessage(String token, String messageId, final int position) {
        addSubscribe(dataManager.readMessage(token, messageId)
                .compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) throws Exception {
                        if (commonBean.getCode() == 0) {
                            mView.viewReadMessage(commonBean, position);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.e(throwable.getMessage());
                    }
                }));
    }
}
